/*
 * mod_math.h
 *
 *  Created on: 2025 Feb 17
 *      Author: SFLY
 */

#ifndef XTFOCLIB_MODULES_MOD_MATH_H_
#define XTFOCLIB_MODULES_MOD_MATH_H_
#include "math.h"
/* define sign() function *****************************************************/
#define SIGN(X)         (((X) > 0) ? (1) : ((X) < 0 ? (-1) : (0)))

#define SQRT3           1.7320508f          /* 1.732 */
#define INV_SQRT3       0.5773503f          /* 0.577 */
#define TwoINV_SQRT3    1.1547005f          /* 1.155 */
#define INV_3           0.3333333f          /* 1/3 */
#define MATH_TWO_PI     6.2831852f          /* 2*pi */
#define INV_MATH_TWO_PI 0.1591549f
#define MATH_PI         3.1415927f
#define INV_MATH_PI     0.3183099f
#define ANGLE2RAD       0.0174533f
#define _constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
extern const float f32SinTbl[257];
/**
 ******************************************************************************
 ** \brief sine function by look-up table
 **
 ** \param [in] i32Q24_angle: Q24 format P.U. angle
 **
 ** \retval     Q15-sin(angle)
 ******************************************************************************/
extern float __attribute__((optnone)) Math_Sin(float f32angle);

/**
 ******************************************************************************
 ** \brief cosine function by look-up table
 **
 ** \param [in] i32Q24_angle: Q24 format P.U. angle
 **
 ** \retval     Q15-cos(angle)
 ******************************************************************************/
extern float __attribute__((optnone)) Math_Cos(float f32angle);

/**
 ******************************************************************************
 ** \brief sine/cosine function by look-up table
 **
 ** \param [in] i32Q24_angle: Q24 format P.U. angle
 **
 ** \retval     f32-sin(angle), f32-cos(angle)
 ******************************************************************************/
extern void __attribute__((optnone)) Math_SinCos(float f32angle, float *f32sin, float *f32cos);



/**
 ******************************************************************************
 ** \brief Math_Atan2(x, y) function
 **         1. the input x, and y should have the same Q-format
 **         2. both x and y should less than 2^24, otherwise precision degrades
 **
 ** \param [in] i32_x: x-axis value, < 2^24
 ** \param [in] i32_y: y-axis value, < 2^24
 **
 ** \retval     int32_t i32Q24_angle: Q24 format P.U. angle
 ******************************************************************************/
extern float __attribute__((optnone)) Math_Atan2(float f32x, float f32y);

float normalizeAngle(float angle);

#endif /* XTFOCLIB_MODULES_MOD_MATH_H_ */
